package cn.wyl.basic.controller;

import cn.wyl.basic.exception.BusinessException;
import cn.wyl.basic.service.VerifyService;
import cn.wyl.basic.util.AxiousResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

//异步请求都要用@ResponseBody
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private VerifyService verifyService;
    //发送图形验证码接口
    // 前端：    this.$http.get("/verifyCode/image/" + key
    @GetMapping("/image/{key}")
    public AxiousResult imageVerifyCode(@PathVariable("key") String key){
        try {
            String base64Str = verifyService.getImgCode(key);
            return new AxiousResult(true,"成功",base64Str);
        } catch (BusinessException e) {
            return new AxiousResult(false,"验证码获取失败"+e.getMessage());
        }catch (Exception e) { //500
            e.printStackTrace();
            return new AxiousResult(false,"系统异常");
        }


    }
    //发送手机验证码接口
    //前端：this.$http.post("/verifyCode/smsCode",param)
    /*param = {
            phone: this.phoneUserForm.phone,
            imageCode: this.phoneUserForm.imageCode,
            //从localStorage中获取key：phoneRegisterCodeKey
            imageCodeKey: localStorage.getItem("phoneRegisterCodeKey")
            };
       返回axiousresult （msg）
     */
    @PostMapping("/smsCode")
    public AxiousResult sendSmsCode(@RequestBody Map<String,Object> paramMap){
        try {
            verifyService.sendSmsCode(paramMap);
            return new AxiousResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AxiousResult(false,"发送失败!"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AxiousResult(false,"系统异常,请稍后重试!");
        }

    }

    /**
     * 微信发送手机验证码
     * @param paramMap
     * @return
     */
    @PostMapping("/binderSmsCode")
    public AxiousResult binderSmsCode(@RequestBody Map<String,Object> paramMap){
        try {
            verifyService.binderSmsCode(paramMap);
            return new AxiousResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AxiousResult(false,"发送失败!"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AxiousResult(false,"系统异常,请稍后重试!");
        }

    }

}
